﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey.Web;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Display the template directories
    /// </summary>
    public partial class LibraryDirectory : PageBase
    {
    

        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetupSecurity();
            LocalizePage();
            MessageLabel.Visible = false;

            if (!Page.IsPostBack)
            {
           
                BindData();
            }

            DeleteLibraryButton.Attributes.Add("onClick",
                "javascript:if(confirm('" + GetPageResource("DeleteLibraryConfirmationMessage") + "')== false) return false;");
        }

        private void LocalizePage()
        {
            LibraryTitle.Text = GetPageResource("LibraryTitle");
            AddNewLibraryButton.Text = GetPageResource("AddNewLibraryButton");
            DeleteLibraryButton.Text = GetPageResource("DeleteLibraryButton");
            UpdateLibraryButton.Text = GetPageResource("UpdateLibraryButton");
            LibraryNameLabel.Text = GetPageResource("LibraryNameLabel");
        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.AccessLibrary, true);
            AddNewLibraryButton.Visible = NSurveyUser.Identity.IsAdmin || CheckRight(NSurveyRights.ManageLibrary, false);
        }

        /// <summary>
        /// Get the current DB stats and fill 
        /// the label with them
        /// </summary>
        private void BindData()
        {
            // Retrieve the library data
            LibraryData libraryData =
                new Libraries().GetLibraries();
            LibraryDataList.DataSource = libraryData;
            LibraryDataList.DataMember = "Libraries";
            LibraryDataList.DataBind();
        }


        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.LibraryDataList.ItemCreated += new System.Web.UI.WebControls.DataListItemEventHandler(this.LibraryDataList_ItemCreated);
            this.LibraryDataList.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.LibraryDataList_ItemCommand);

        }
        #endregion

        protected void AddLibraryButton_Click(object sender, System.EventArgs e)
        {
            if (LibraryNameTextBox.Text.Length == 0)
            {
                MessageLabel.Text = GetPageResource("MissingLibraryNameMessage");
                MessageLabel.Visible = true;
                return;
            }
            else
            {
                LibraryData libraryData = new LibraryData();
                LibraryData.LibrariesRow library = libraryData.Libraries.NewLibrariesRow();
                library.LibraryName = LibraryNameTextBox.Text;
                libraryData.Libraries.Rows.Add(library);
                new Library().AddLibrary(libraryData);
                LibraryNameTextBox.Text = string.Empty;
                BindData();
                MessageLabel.Text = GetPageResource("AddedLibraryNameMessage");
                MessageLabel.Visible = true;
            }
        }

        private void LibraryDataList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            LibraryData library = new Libraries().GetLibraryById(int.Parse(e.CommandArgument.ToString()));
            LibraryNameTextBox.Text = library.Libraries[0].LibraryName;
            ViewState["LibraryId"] = library.Libraries[0].LibraryId;
            AddLibraryButton.Visible = false;
            AddLibraryTitle.Text = GetPageResource("UpdateLibraryTitle");
            UpdateLibraryButton.Visible = true;
            DeleteLibraryButton.Visible = true;
            editplaceholder.Visible = true;
        }

        protected void UpdateLibraryButton_Click(object sender, System.EventArgs e)
        {
            if (LibraryNameTextBox.Text.Length == 0)
            {
                MessageLabel.Text = GetPageResource("MissingLibraryNameMessage");
                MessageLabel.Visible = true;
                editplaceholder.Visible = true;
                return;
            }
            else
            {
                LibraryData libraryData = new LibraryData();
                LibraryData.LibrariesRow library = libraryData.Libraries.NewLibrariesRow();
                library.LibraryId = int.Parse(ViewState["LibraryId"].ToString());
                library.LibraryName = LibraryNameTextBox.Text;
                libraryData.Libraries.Rows.Add(library);
                new Library().UpdateLibrary(libraryData);
                LibraryNameTextBox.Text = string.Empty;
                BindData();
                MessageLabel.Text = GetPageResource("UpdatedLibraryNameMessage");
                MessageLabel.Visible = true;
                editplaceholder.Visible = false;
            }
        }

        protected void DeleteLibraryButton_Click(object sender, System.EventArgs e)
        {
            new Library().DeleteLibrary(int.Parse(ViewState["LibraryId"].ToString()));
            BindData();
            MessageLabel.Text = GetPageResource("DeletedLibraryNameMessage");
            MessageLabel.Visible = true;
            editplaceholder.Visible = false;
            LibraryNameTextBox.Text = string.Empty;
        }

        protected void AddNewLibraryButton_Click(object sender, System.EventArgs e)
        {
            UpdateLibraryButton.Visible = false;
            DeleteLibraryButton.Visible = false;
            AddLibraryButton.Visible = true;
            AddLibraryTitle.Text = GetPageResource("AddLibraryTitle");
            editplaceholder.Visible = true;
        }

        private void LibraryDataList_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            e.Item.FindControl("EditDirectoryHyperLink").Visible =
                NSurveyUser.Identity.IsAdmin || CheckRight(NSurveyRights.ManageLibrary, false);
        }

    }

}
